package ch.bfh.abcm.rems.display;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JFrame;

import ch.bfh.abcm.rems.controller.MulticastController;
import ch.bfh.abcm.rems.display.subpanels.history.HistoryPanel;

public class Mainframe extends JFrame {

    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    private static final int WIDTH = 500;
    private static final int HEIGHT = 800;
    private static final Dimension mainframeSize = new Dimension(WIDTH, HEIGHT);
    private static final String TITLE = "REMS Display";

    private HistoryPanel historyPanel;
    private Point initialClick;

    public Mainframe(MulticastController multicastController) {

        this.setSize(mainframeSize);
        this.setTitle(TITLE);
        this.setResizable(true);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setBackground(Color.BLACK);
        this.setLayout(new BorderLayout());
        this.setUndecorated(true);

        this.historyPanel = new HistoryPanel(WIDTH / 2, HEIGHT);

        multicastController.addObserver(historyPanel);

        this.add(historyPanel, BorderLayout.CENTER);

        addMouseListener(new MouseAdapter() {
            public void mousePressed(MouseEvent e) {
                initialClick = e.getPoint();
                getComponentAt(initialClick);
            }
        });
        this.setVisible(true);

        addMouseMotionListener(new MouseMotionAdapter() {
            @Override
            public void mouseDragged(MouseEvent e) {

                // get location of Window
                int thisX = getLocation().x;
                int thisY = getLocation().y;

                // Determine how much the mouse moved since the initial click
                int xMoved = (thisX + e.getX()) - (thisX + initialClick.x);
                int yMoved = (thisY + e.getY()) - (thisY + initialClick.y);

                // Move window to this position
                int X = thisX + xMoved;
                int Y = thisY + yMoved;
                setLocation(X, Y);
            }
        });
    }
}
